//
//  KY23AdvertView.m
//  KYAdvertSDK
//
//  Created by 罗新 on 2017/6/28.
//  Copyright © 2017年 蔡正军. All rights reserved.
//

#import "KY23AdvertView.h"
#import "KYAdvertSDK.h"
@interface KY23AdvertView()
@property (nonatomic, strong) UIImageView                   *imgView;
@property (nonatomic, strong) NSMutableDictionary           *dict;
@end

@implementation KY23AdvertView

#pragma mark - Init
/**
 初始化
 */
- (instancetype)init {
    self = [super init];
    if (self) {
        self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kCurrentScreenWidth(60), kCurrentScreenWidth(60))];
        [self addSubview:self.imgView];
        [self createTap];
    }
    return self;
}

#pragma mark - Public methods
/**
 填充视图内容
 */
- (void)createContent:(NSDictionary *)dataDic blockHeight:(void (^)(NSInteger))viewHeight {
    self.dict = [NSMutableDictionary dictionaryWithDictionary:dataDic];
    NSString *pathDocument = [KYSystemInfo getSaveAdvertImagePath];
//    KYLog(@"悬浮广告数据：%@",dataDic);
    if (dataDic && dataDic[@"data"]) {
        NSString *imagePath = [pathDocument stringByAppendingString:[NSString stringWithFormat:@"%@.png",self.dict[@"data"][@"adsid"]]];
        UIImage *cacheImage = [UIImage imageWithContentsOfFile:imagePath];
        __weak typeof(self) _self = self;
        [self.imgView KYSD_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:dataDic[@"data"][@"imageurl"]] placeholderImage:cacheImage options:KYSDWebImageAllowInvalidSSLCertificates progress:nil completed:^(UIImage *image, NSError *error, KYSDImageCacheType cacheType, NSURL *imageURL) {
            if (image) {
                __strong typeof(_self) self = _self;
                [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
                CGSize size = image.size;
                CGFloat height= size.height;
                CGFloat width = size.width;
                if (viewHeight) viewHeight(kDeviceWidth / width * height);
                self.imgView.frame = CGRectMake(0, 0, kCurrentScreenWidth(60), kCurrentScreenWidth(60));
                [self postPVData];
            }else{
            }
        }];
    }
}

/**
 创建广告视图点击手势
 */
- (void)createTap {
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeOtherPage)];
    self.imgView.userInteractionEnabled = YES;
    [self.imgView addGestureRecognizer:tap];
}

/**
 移除视图
 */
- (void)remove23View {
    self.window.hidden = YES;
    [self.superview.superview removeFromSuperview];
}

#pragma mark - Response method
/**
 统计PV数据
 */
- (void)postPVData {
    //发送统计数据
    [[NSNotificationCenter defaultCenter] postNotificationName:@"advertPVData" object:self.dict[@"data"]];
}
/**
 广告视图事件跳转 同时触发post页面统计请求
 */
- (void) changeOtherPage{
//    [self.dict setValue:kOnClick forKey:@"clickStatus"];
//    [self.dict setObject:@"3" forKey:@"viewTime"];
    [self remove23View];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickView:)]) {
        [self.delegate clickView:self.dict[@"data"]];
    }
}

#pragma mark - Lazy load
- (NSMutableDictionary *)dict {
    if (!_dict) {
        _dict = [[NSMutableDictionary alloc] init];
    }
    return _dict;
}
@end
